#!/usr/bin/env python

from dl_manager_class import DLManager

import os, sys

def browse_files ( liwe , id_file = 0, name = '', descr = '', filters_tags = '', fext = '', page_view = '', order_by = '', id_obj = 0,  date_start = None, date_end = None, module ='', _X_LINES = 10, _X_START_POINT = 0, _X_PAGE = 0, kwargs = 0 ):
	"""
        :INPUT:
		- id_file: file id						SAFE_NUMBER
                - name:  Nome del file						SAFE_DB_STRING
                - descr:   Descrizione						SAFE_DB_STRING
                - module:   modul						SAFE_DB_STRING
		- id_obj:   id_oggetto						SAFE_NUMBER
                - filters_tags: elenco tags					SAFE_DB_STRING
                - fext: estensione file						SAFE_DB_STRING
                - date_start:							SAFE_DATE
                - date_end:							SAFE_DATE
                - page_view: tipo di visualizzazione (icons,list,details)	SAFE_DB_STRING
                - order_by: order by ( name, ext, created ) name default	SAFE_DB_STRING
		- _X_LINES:							SAFE_NUMBER
                - _X_START_POINT:						SAFE_NUMBER
                - _X_PAGE:							SAFE_NUMBER
	"""
	dm = DLManager ( liwe )
	
   	if _X_START_POINT:
                start_point = _X_START_POINT
        else:
                start_point = _X_PAGE * _X_LINES

        limit = _X_LINES

        vals = dm.browse_files ( id_file = id_file, name = name, descr = descr, filters_tags = filters_tags, fext = fext, 
				page_view = page_view, id_obj = id_obj, module = module , date_start = date_start, date_end = date_end, 
				order_by = order_by, start_pos = start_point, limit = limit, kwargs = kwargs )

	if not vals: 
		return { 'rows' : [], 'from_row' : 0, 'to_row' : 0, 'lines' : 0 }

        res = { "rows" : vals [ 'rows' ] }

        count = 0
        for v in vals [ 'results' ]:
                v [ "_pos" ] = start_point + count
                res [ "row%d" % count ] = v
                count += 1

        res [ 'from_row' ] = start_point
        res [ 'to_row' ] = start_point + count
        res [ 'lines' ] = count
	#res [ 'results' ] = vals [ 'results' ]

        return res

def get_files ( liwe, id_file = 0, name = '', descr = '', filters_tags = '', fext = '', page_view = '', order_by = '', id_obj = 0,  module ='', date_start = None, date_end = None, 
		start_point = 0, limit = 0, kwargs = 0 ):

	"""
        :INPUT:
		- id_file: file id						SAFE_NUMBER
                - name:  Nome del file						SAFE_DB_STRING
                - descr:   Descrizione						SAFE_DB_STRING
                - module:   modulo						SAFE_DB_STRING
		- id_obj:   id_oggetto						SAFE_STRING
                - filters_tags: elenco tags					SAFE_DB_STRING
                - fext: estensione file						SAFE_DB_STRING
                - page_view: tipo di visualizzazione (icons,list,details)	SAFE_DB_STRING
                - order_by: order by ( name, ext, created ) name default	SAFE_DB_STRING
                - start_point:							SAFE_NUMBER
                - limit:							SAFE_NUMBER
	"""
	dm = DLManager ( liwe )

        vals = dm.browse_files ( id_file = id_file, name = name, descr = descr, filters_tags = filters_tags, fext = fext, 
						page_view = page_view, id_obj = id_obj, module = module , date_start = date_start, date_end = date_end,
						order_by = order_by, start_pos = start_point, limit = limit, kwargs = kwargs )
	if not vals:
		vals = {} 
	return  vals

def _define_mime_type ( liwe, path_name = '' ):
	"""
        :INPUT:
                - path_name:  percorso del file completo	SAFE_DB_STRING
	"""
	dm = DLManager ( liwe )
	mime = dm._define_mime_type ( path_name )
	return { "mimetype" : mime }	

def file_details ( liwe, path_name = '', id_file = '' ):
	"""
        :INPUT:
                - path_name:  percorso del file completo	SAFE_DB_STRING
                - id:  id del file				SAFE_NUMBER
	"""
	dm = DLManager ( liwe )
	file_dets = dm.file_details ( path_name, id_file )
	return { "details" : file_dets }

def download_file ( liwe, path_name = '', file_name = '' ):
	"""
        :INPUT:
                - path_name:  percorso del file completo	SAFE_DB_STRING
                - file_name:  nome o id del file		SAFE_DB_STRING
	"""
	dm = DLManager ( liwe )
	dm.download_file ( path_name, file_name )

def _get_ext_list ( liwe ):
	dm = DLManager ( liwe )
	ext_list = dm._get_ext_list ()	
	return { "ext_list" : ext_list }

def _get_mods_list ( liwe ):
	dm = DLManager ( liwe )
	ext_mods = dm._get_mods_list ()	
	return { "mods_list" : ext_mods }

def get_templates ( liwe ):
	n = DLManager ( liwe )
	return { "templates" : n.get_templates () }
